import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.apache.commons.io.FileUtils;

public class Util {
	public static String getGoodUrl ( String url ) {
		url = ( url.indexOf ( "?" ) > -1 ? url.substring ( 0 , url.indexOf ( "?" ) ) : url );
		url = ( url.indexOf ( "#" ) > -1 ? url.substring ( 0 , url.indexOf ( "#" ) ) : url );
		return url;
	}
	
	public static String getStrFromFile ( String file ) {
		return readFileByLines ( file );
	}
	
	public static String formatJson ( String json ) throws Exception {
		return JsonFormatTool.formatJson ( json , " " );
	}
	
	public static String readFileByLines ( String fileName ) {
		File file = new File ( fileName );
		StringBuilder sb = new StringBuilder ( );
		BufferedReader reader = null;
		String tempString = null;
		try {
			reader = new BufferedReader ( new FileReader ( file ) );
			while ( ( tempString = reader.readLine ( ) ) != null ) {
				sb.append ( tempString );
			}
			return sb.toString ( );
		} catch ( Exception e ) {
			e.printStackTrace ( );
		} finally {
			if ( reader != null ) {
				try {
					reader.close ( );
					reader = null;
				} catch ( IOException e1 ) {
					e1.printStackTrace ( );
				}
			}
		}
		return "";
	}
	
	public static boolean mkdir ( String dir ) {
		return new File ( dir ).exists ( ) || new File ( dir ).mkdirs ( );
	}
	
	public static void write ( File file , String string ) {
		try {
			BufferedWriter bfw = new BufferedWriter ( new FileWriter ( file ) );
			bfw.write ( string );
			bfw.close ( );
		} catch ( IOException e ) {
			e.printStackTrace ( );
		}
	}
	
	public static boolean downloadFromUrl ( String remoteFilePath , String localFilePath ) throws MalformedURLException , IOException {
		System.out.println ( "downloadFile:" + remoteFilePath + "==>" + localFilePath );
		FileUtils.copyURLToFile ( new URL ( remoteFilePath ) , new File ( localFilePath ) );
		return true;
	}
	
	public static void copyDirectiory ( String sourceDir , String targetDir ) throws IOException {
		FileUtils.copyDirectory ( new File ( sourceDir ) , new File ( targetDir ) );
	}
	
	public static void writeTemplate ( String page , Map < String , String > templateStr ) {
		String str = getStrFromFile ( page );
		for ( String t1 : templateStr.keySet ( ) ) {
			str = str.replace ( t1 , templateStr.get ( t1 ) );
		}
		write ( new File ( page ) , str );
	}
	
	public static void zip ( String zipFile , String dir , Set < String > notzipfile ) throws Exception {
		ZipUtil.zip ( zipFile , dir , notzipfile );
	}
	
	// public static void rename(String oldFile, String newFile) {
	// new File(oldFile).renameTo(new File(newFile));
	// }
	
	public static void copy ( String srcFile , String destFile ) throws IOException {
		FileUtils.copyFile ( new File ( srcFile ) , new File ( destFile ) );
	}
	
	public static String defineHtml ( String setname ) {
		Map < String , String > chars = new HashMap < String , String > ( );
		chars.put ( "\\" , "_" );
		chars.put ( "/" , "_" );
		chars.put ( "|" , "_" );
		chars.put ( ">" , "_" );
		chars.put ( "<" , "_" );
		chars.put ( ":" , "_" );
		chars.put ( "*" , "_" );
		chars.put ( "?" , "_" );
		chars.put ( "\"" , "_" );
		chars.put ( "\'" , "_" );
		for ( String str : chars.keySet ( ) ) {
			if ( setname.indexOf ( str ) >= 0 ) setname = setname.replaceAll ( str , chars.get ( str ) );
		}
		return setname;
	}
	
}
